﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;              //命名空间
using System.Data.SqlClient;    //数据库头文件
using System.Data.Sql;         //数据库头文件
using DATA_operate_;          //自定义数据库操作类引用
using System.IO;

public partial class design_page_teacher_Default : System.Web.UI.Page
{
    DATA_operate data_operate_in_upload = new DATA_operate();   //创建数据库操作类对象
   

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            upload_file.Style.Add("display", "none");   //隐藏上传文件控件组
            showjianjie.Style.Add("display", "none");   //隐藏上传文件控件组




            string sql_show_all_updata = "select _Uploaderid,_Uploadername,_Updataname,_Uploadtime,_Updowncount from _updata_info";
            data_operate_in_upload.Data_bind(sql_show_all_updata, GridView_show_data, "_Uploaderid");

        }
    }

    protected void Button1_Click(object sender, EventArgs e)        //百度一下
    {
        Button4_Click(sender, e);


        HttpCookie cookie = Request.Cookies["teaid"];
        string str_Tid = cookie.Value.ToString();

        string search_type = DropDownList1.SelectedItem.Text;  //搜索类型
        string search_condition = TextBox1.Text;
        switch (search_type)
        {
            case "根据资料名称查询":
                string sql_show_search1 = "select _Uploaderid,_Uploadername,_Updataname,_Uploadtime,_Updowncount from _updata_info where _Updataname like '%" + search_condition + "%'";
                data_operate_in_upload.Data_bind(sql_show_search1, GridView_show_data, "_Uploaderid");
                break;
            case "根据发布者名称查询":
                string sql_show_search2 = "select _Uploaderid,_Uploadername,_Updataname,_Uploadtime,_Updowncount from _updata_info where _Uploadername like '%" + search_condition + "%'";
                data_operate_in_upload.Data_bind(sql_show_search2, GridView_show_data, "_Uploaderid");
                break;
            case "我上传的资料":
                string sql_show_search3 = "select _Uploaderid,_Uploadername,_Updataname,_Uploadtime,_Updowncount from _updata_info where _Uploaderid ='" + str_Tid + "'";
                data_operate_in_upload.Data_bind(sql_show_search3, GridView_show_data, "_Uploaderid");
                break;

        }
    }
    protected void Button2_Click(object sender, EventArgs e)   //我要上传
    {
        if (Button2.Text == "我要上传")
        {
            Is_GridView_show.Style.Add("display", "none");

            TextBox1.Enabled = false;
            DropDownList1.Enabled = false;
            Button1.Enabled = false;
            Button2.Text = "我要下载";
            upload_file.Style.Add("display", "block");   //隐藏上传文件控件组

        }
        else
        {
            Is_GridView_show.Style.Add("display", "block");

            TextBox1.Enabled = true;
            DropDownList1.Enabled = true;
            Button1.Enabled = true;
            Button2.Text = "我要上传";
            upload_file.Style.Add("display", "none");   //隐藏上传文件控件组
            Response.Redirect(Request.RawUrl);
        }

    }
    protected void Button3_Click(object sender, EventArgs e)   //上传
    {
        try
        {
            string upload_file_name = FileUpload2.FileName;   //带后缀的文件名
            string upload_file_postfix = upload_file_name.Substring(upload_file_name.LastIndexOf("."));//后缀
            string path = Server.MapPath("../../acceptfile/") + upload_file_name;  //设置上传到的路径





            FileUpload2.PostedFile.SaveAs(path);   //上传


            HttpCookie cookie = Request.Cookies["teaid"];
            string str_sid = cookie.Value.ToString();


            string _Uploaderid = str_sid;
            string _Uploadername = data_operate_in_upload.dataread("_teacher", "_Tname", "_Tid", str_sid);
            string _Updataname = upload_file_name;
            string _Updata_brief_intro;
            if (TextBox3.Text == "")
                _Updata_brief_intro = "无简介信息";
            else
                _Updata_brief_intro = TextBox3.Text;
            string _Updatasavrpath = path;
            string _Uploadtime = DateTime.Now.ToLocalTime().ToString();



            if (data_operate_in_upload.dataadd("_updata_info", "_Uploaderid,_Uploadername,_Updataname,_Updata_brief_intro,_Updatasavrpath,_Uploadtime", _Uploaderid + "," + _Uploadername + "," + _Updataname + "," + _Updata_brief_intro + "," + _Updatasavrpath + "," + _Uploadtime))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('上传成功');", true);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('写入数据库失败');", true);
            }




        }
        catch (System.Exception ex)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "right", "window.alert('上传失败');", true);
        }










    }

    protected void Gridview_show_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "jianjie")
        {
            showjianjie.Style.Add("display", "block");
            Button4.Visible = true;

            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvr = GridView_show_data.Rows[index];
            string str_uploadid = ((Label)gvr.FindControl("dLabel_uploadid")).Text;
            string str_uploadname = ((Label)gvr.FindControl("dLabel_filename")).Text;
            TextBox2.Text = data_operate_in_upload.dataread("_updata_info", "_Updata_brief_intro", "_Uploaderid", str_uploadid, "_Updataname", str_uploadname);

        }
        if (e.CommandName == "xiazai")
        {

            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow gvr = GridView_show_data.Rows[index];
            string str_uploadid = ((Label)gvr.FindControl("dLabel_uploadid")).Text;
            string str_uploadname = ((Label)gvr.FindControl("dLabel_filename")).Text;
            string kk = data_operate_in_upload.dataread("_updata_info", "_Updowncount", "_Uploaderid", str_uploadid, "_Updataname", str_uploadname);
       
          
            int count = int.Parse(kk) + 1;
            string sql_downadd_one = "update _updata_info set _Updowncount = " + count + " where _Uploaderid = '" + str_uploadid + "' and _Updataname = '" + str_uploadname + "'";
            if (!(data_operate_in_upload.sqlcmd(sql_downadd_one).ExecuteNonQuery() > 0))
            {
                return;
            }


            string path = Server.MapPath(Request.ApplicationPath + "/acceptfile/" + str_uploadname);

            System.IO.FileInfo file = new System.IO.FileInfo(path);
            Response.Clear();
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;


            Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + str_uploadname + "\"");

            Response.AppendHeader("Content-Length", file.Length.ToString());

            Response.ContentType = "application/ms-excel";

            Response.WriteFile(file.FullName);




            Response.End();


            //TextBox2.Text = data_operate_in_upload.dataread("_updata_info", "_Updata_brief_intro", "_Uploaderid", str_uploadid, "_Updataname", str_uploadname);

            //下载后   下载次数加1



        }
    }
    protected void Button4_Click(object sender, EventArgs e)   //关闭简介
    {
        Button4.Visible = false;
        showjianjie.Style.Add("display", "none");
    }
    protected void Button5_Click(object sender, EventArgs e)   //返回
    {
        Button2_Click(sender, e);
        Button2.Text = "我要上传";

    }
}